(function ($) {
  "use strict";
  $.fn.loadend = function (option) {
    var self = $(this);
    var defaults = {
      maxmove: 85
    };
    var params = $.extend({},defaults,option);
    var getOffset = function (container) {
      var tagName = container[0].tagName.toUpperCase()
      var scrollTop 
      if (tagName === 'BODY' || tagName === 'HTML') {
        scrollTop = container.scrollTop() || $(window).scrollTop()
      } else {
        scrollTop = container.scrollTop()
      }
      var offset = container.scrollHeight() - ($(window).height() + scrollTop)
      return offset
    }
    var startY;
    var tagN = self[0].tagName.toUpperCase();
    var tip = $('<div class="gzl-loadend-tip">木有啦，快放手了啦~</div>');
    if (tagN === 'BODY') {
      self.addClass("gzl-loadend").append(tip);
    } else {
      self.addClass("gzl-loadend").after(tip);
    }
    self.on($.touchEvents.start, function (e) {
      var p  = $.getTouchPosition(e);
      startY = p.y;
    });
    self.on($.touchEvents.move, function (e) {
      var p = $.getTouchPosition(e);
      $(this).addClass("touching")
      var refresh = $(".gzl-pullrefresh__loading");
      if (getOffset(self) <= (refresh.length>0?refresh.height():0)) {
        self.addClass("gzl-loadend");
        var diffY = 0;
        if (startY - p.y > params.maxmove) {
          diffY = params.maxmove;
        } else {
          diffY = startY - p.y;
        }
        // $(this).css("transform", "translateY(-" + diffY + "px)");
        $(this).css({
          'position': 'relative',
          'top': -diffY
        });
        tip.addClass("touching").css("height",diffY);
      }else{
        self.removeClass("gzl-loadend");
      }
    });
    self.on($.touchEvents.end, function (e) {
      $(this).removeClass("touching")
      // $(this).css("transform", "");
      $(this).css({
        'position': 'relative',
        'top': 0
      });
      tip.removeClass("touching").css({"height":"0","overflow":"hidden"});
    });
  }
})($);
(function ($) {
  "use strict";
  var getOffset = function (container) {
    var tagName = container[0].tagName.toUpperCase()
    var scrollTop 
    if (tagName === 'BODY' || tagName === 'HTML') {
      scrollTop = container.scrollTop() || $(window).scrollTop()
    } else {
      scrollTop = container.scrollTop()
    }
    var offset = container.scrollHeight() - ($(window).height() + scrollTop)
    // console.log(offset)
    return offset
  }
  var Infinite = function(el, distance) {
    this.container = $(el);
    this.container.data("infinite", this);
    this.distance = distance || 50;
    this.attachEvents();
  }
  Infinite.prototype.scroll = function() {
    var container = this.container;
    this._check();
  }
  Infinite.prototype.attachEvents = function(off) {
    var el = this.container;
    if ($(".gzl-loading-tip", el).length < 1) {
      el.append('<div class="gzl-loading-tip">加载中……</div>');
    } else {
      $(".gzl-loading-tip", el).remove();
      this.loadend();
    }
    var scrollContainer = (el[0].tagName.toUpperCase() === "BODY" ? $(document) : el);
    scrollContainer[off ? "off" : "on"]("scroll", $.proxy(this.scroll, this));
  };
  Infinite.prototype.detachEvents = function(off) {
    this.attachEvents(true);
  }
  Infinite.prototype._check = function() {
    var offset = getOffset(this.container);
    if(Math.abs(offset) <= this.distance) {
      this.container.trigger("infinite");
    }
  }
  Infinite.prototype.loadend = function () {
    var el = this.container;
    var tip = $('<div class="gzl-loadend-tip">木有啦，快放手了啦~</div>');
    el.addClass("gzl-loadend").after(tip);
    el.on($.touchEvents.start, function(e){
      var p  = $.getTouchPosition(e);
      this.startY = p.y;
    });
    el.on($.touchEvents.move, function (e) {
      var p = $.getTouchPosition(e);
      el.addClass("touching")
      var refresh = $(".gzl-pullrefresh__loading");
      if (getOffset(el) <= (refresh.length>0?refresh.height():0)) {
        el.addClass("gzl-loadend");
        if (this.startY - p.y > 85) {
          this.diffY = 85;
        } else {
          this.diffY = this.startY - p.y;
        }
        // el.css("transform", "translateY(-" + this.diffY + "px)");
        el.css({
          'position': 'relative',
          'top': -this.diffY
        });
        tip.addClass("touching").css("height",this.diffY);
      }else{
        el.removeClass("gzl-loadend");
      }
    });
    el.on($.touchEvents.end, function (e) {
      el.removeClass("touching")
      // el.css("transform", "");
      el.css({
        'position': 'relative',
        'top': 0
      });
      tip.removeClass("touching").css({"height":"0","overflow":"hidden"});
    });
  }
  var infinite = function(el) {
    attachEvents(el);
  }
  $.fn.infinite = function(distance) {
    return this.each(function() {
      new Infinite(this, distance);
    });
  }
  $.fn.destroyInfinite = function() {
    return this.each(function() {
      var infinite = $(this).data("infinite");
      if(infinite && infinite.detachEvents) infinite.detachEvents();
    });
  }
})($);
